package com.usian.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import com.usian.feign.UserFeign;
import com.usian.pojo.Emp;
import com.usian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class UserController {
    //从eureka注册中心获取服务端的ip、端口、要调用的服务
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    UserFeign userFeign;
    //访问Rest服务的客户端
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping(value="consumer/user/{id}",method= RequestMethod.GET)
    public User getUserby(@PathVariable Integer id){
        //找微服务
        ServiceInstance si = loadBalancerClient.choose("eureka-provider");
        //发起请求
        String url = "http://"+si.getHost()+":"+si.getPort()+"/user/"+id;
        return restTemplate.getForObject(url, User.class);
    }
    /*@RequestMapping(value="consumer/findall",method= RequestMethod.GET)
    public List<Emp> findall(){
        //找微服务
        ServiceInstance si = loadBalancerClient.choose("eureka-provider");
        //发起请求
        String url = "http://"+si.getHost()+":"+si.getPort()+"/findall";
        return restTemplate.getForObject(url,List.class);
    }
    @RequestMapping(value="consumer/emp/{id}",method= RequestMethod.GET)
    public Emp getempbyeid(@PathVariable Integer id){
        //找微服务
        ServiceInstance si = loadBalancerClient.choose("eureka-provider");
        //发起请求
        String url = "http://"+si.getHost()+":"+si.getPort()+"/emp/"+id;
        return restTemplate.getForObject(url, Emp.class);
    }
    @RequestMapping(value="consumer/emp1/{ename}",method= RequestMethod.GET)
    public List<Emp> getempbyename(@PathVariable String ename){
        //找微服务
        ServiceInstance si = loadBalancerClient.choose("eureka-provider");
        //发起请求
        String url = "http://"+si.getHost()+":"+si.getPort()+"/emp1/"+ename;
        return restTemplate.getForObject(url, List.class);
    }
*/
    @HystrixCommand(fallbackMethod = "fallback1")
    @RequestMapping("/consumer/user")
    public User getUser(){


        return userFeign.getUser1();
    }
    // 返回托底数据的方法
    public User fallback1(){
        return new User(5,"我是托底数据Fallback",5);
    }
    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping("user/{id}")
    public User getUser(@PathVariable Integer id){
        return userFeign.getUser(id);
    }
    // 返回托底数据的方法
    public User fallback(Integer id){
        return new User(0,"我是托底数据Fallback",0);
    }

    @RequestMapping("/consumer/findall")
    public List<Emp> findall(){
        return userFeign.findall();
    }
    @RequestMapping("/consumer/emp/{id}")
    public Emp getempbyeid(@PathVariable Integer id){
        return userFeign.getempbyeid(id);
    }
    @RequestMapping("/consumer/emp1/{ename}")
    public List<Emp> getempbyename(@PathVariable String ename){
        return userFeign.getempbyename(ename);
    }
    @RequestMapping("/consumer/list/{nums}")
    public int[] findnums(@PathVariable int[] nums){
        return userFeign.findnums(nums);
    }
    @RequestMapping("/user")
    public User finduser(@RequestBody User user){
        return userFeign.finduser(user);
    }
    @RequestMapping("/consumer/id")
    public Integer findid(@RequestParam int id){
        return userFeign.findid(id);
    }
}
